'''
@author: nick mueller
'''

import ButtonController
import Communication
import TLCam
import exchange_pb2
import util

class InputController:
    '''
    The InputController is responsible for all input events. The controller
    processes the events.
    '''
    
    def __init__(self):
        self.button = ButtonController.ButtonController(self.requestController)
        self.button.startButtonListener()
        
        self.tlc = TLCam.getTLC()
        self.com = Communication.getCommunication()
        self.com.setIncomingListener(self.requestController)
        
    def requestController(self, message):
        '''
        method gets the incoming message (from smartphone) and 
        processes the request.
        
        the method returns a message for the response
        '''
        
        # each incoming message should be a request
        incoming = exchange_pb2.Request()
        incoming.ParseFromString(message)
        
        status = ""
        response = exchange_pb2.Response()
        response.type = response.STATUS
        
        util.getLogger().info('Incoming:' + str(incoming.type))
        
        # process the incoming message
        # --------------------------------------------------
        if(incoming.type == incoming.GET_MOVIE):
            session = self.tlc.getSession(incoming.param)
            if(not(session == None)) :
                if(session.isRendered() == True):
                    # read in and prepare response
                    try:
                        f = open("./" + session.dirName+ "/" + self.tlc.RENDER_FILE, "rb")
                        response.file.file = f.read()
                        response.file.type = "h264"
                        response.type = response.IMAGE
                        status = session.dirName
                    except Exception, e:
                        status = "Cam can not read movie file: " + str(e)
                     
                    response.type = response.MOVIE
                else:
                    status = "Movie still not rendered"
            else:
                status = "Session not available" 

        if(incoming.type == incoming.GET_TEST_IMG):
            if(not(self.tlc.isInProcess())):
                
                # notify user
                preRes = exchange_pb2.Response()
                preRes.status = "Preview capture begin"
                preRes.type = preRes.STATUS
                self.com.send(util.getDelimitMessage(preRes))
                
                # capture image
                self.tlc.createTestImage()
                
                preRes.status = "Image is sending"
                preRes.type = preRes.STATUS
                self.com.send(util.getDelimitMessage(preRes))
                
                # read in and prepare response
                try:
                    f = open("./" + self.tlc.TEST_IMG, "rb")
                    response.file.file = f.read()
                    response.file.type = "jpg"
                    response.type = response.IMAGE
                except Exception, e:
                    status = "Cam can not read test image: " + str(e)
                
            else:
                status = "Cam already in progress"
                
        if(incoming.type == incoming.GET_STATUS):
            status = "Not implemented"

        if(incoming.type == incoming.GET_SESSIONS):
            response.type = response.SESSIONS
            sessions = self.tlc.getSessions()
    
            for s in sessions:
                session = response.sessions.session.add()
                session.dirName = s.dirName
                session.date = s.createDate
                session.numOfFiles = s.numOfFiles
                session.isRendered = s.isRendered()
                session.thumb.file = s.thumb
                session.thumb.type = "jpg"

        if(incoming.type == incoming.SET_MOVIE_TIME):
            try:        
                self.tlc.setCaptureTime(int(incoming.param))
                #self.tlc.setNumOfCaptureImages(int(incoming.param))
                status = "Changed capture time to " + str(incoming.param) + " seconds"
            except Exception, e:
                status = str(e)

        if(incoming.type == incoming.SET_FPS):
            try:
                self.tlc.setFPS(int(incoming.param))
                status = "Change FPS"
            except Exception, e:
                status = str(e)
            
        if(incoming.type == incoming.SET_TIME_PER_IMG):
            try: 
                self.tlc.setTimePerPic(int(incoming.param))
                status = "Changed time per pic to " + str(incoming.param) + " seconds"
            except Exception, e:
                status = str(e)
            
        if(incoming.type == incoming.START_CAPTURE):
            try: 
                session = self.tlc.createSession(incoming.timestamp)
                self.tlc.startCapture(session)
                status = "Session created ("+session+"), Capture started"
            except Exception, e:
                status = str(e)

        if(incoming.type == incoming.START_MOVIE_CPT):
            try: 
                session = self.tlc.createSession(incoming.timestamp)
                self.tlc.startMovie(session, 15)
                status = "Session created ("+session+"), Movie capture started"
            except Exception, e:
                status = str(e)
            
        if(incoming.type == incoming.START_RENDER):
            try: 
                self.tlc.startRender(incoming.param)
                status = "Rendering started ("+incoming.param+")"
            except Exception, e:
                status = str(e)
        
        if(incoming.type == incoming.DEL_IMAGES):
            status = "Not implemented"

        if(incoming.type == incoming.DEL_SESSION):
            try: 
                self.tlc.deleteSession(incoming.param)
                status = "Session '"+incoming.param+"' successful deleted"
            except Exception, e:
                status = str(e)
                
        # set status and send response
        response.status = status
        return response
